package org.fujene.memoryblock.content;

import java.nio.charset.Charset;
import org.fujene.memoryblock.BlockPackage;
import org.fujene.structure.LookupPointer;

public class ContentPackage implements BlockPackage{
	private LookupPointer current;
	private long ID;
	private LookupPointer[] appendices;
	private String content;
	private int cluster;
	private byte[] contentbyte;

	private ContentPackage(){}

	public static ContentPackage setPackage(int appendixSize, int block, int point){
		ContentPackage newpkg = new ContentPackage();
		newpkg.current = LookupPointer.valueOf(block, point);
		newpkg.appendices = new LookupPointer[appendixSize];
		newpkg.contentbyte = new byte[0];
		return newpkg;
	}

	public static ContentPackage setPackage(LookupPointer[] appendix, String content){
		ContentPackage newpkg = new ContentPackage();
		newpkg.appendices = appendix;
		newpkg.content = content;
		newpkg.contentbyte = content.getBytes(Charset.forName("UTF-8"));
		return newpkg;
	}

	public ContentPackage cluster(int cluster){
		this.cluster = cluster;
		return this;
	}

	public ContentPackage current(LookupPointer ptr){
		this.current = ptr;
		return this;
	}

	public ContentPackage appendix(LookupPointer ptr, int index){
		if(index >= this.appendices.length) return null;
		this.appendices[index] = ptr;
		return this;
	}

	public ContentPackage ID(long newID){
		this.ID = newID;
		return this;
	}

	public ContentPackage content(String content){
		this.content = content;
		this.contentbyte = content.getBytes(Charset.forName("UTF-8"));
		return this;
	}

	public ContentPackage contentraw(byte[] content){
		this.contentbyte = content;
		this.content = new String(content, Charset.forName("UTF-8"));
		return this;
	}

	public LookupPointer getAppendix(int index){
		return appendices[index];
	}

	public LookupPointer getCurrent(){
		return current;
	}

	public long getID(){
		return ID;
	}

	public String getContent(){
		return content;
	}

	public byte[] getContentraw(){
		return contentbyte;
	}

	public int getContentLength(){
		return contentbyte.length;
	}

	public int getCluster(){
		return cluster;
	}

	@Override
	public int length(){
		return (appendices.length * PTR) + LONG + STRLEN + contentbyte.length;
	}
}
